﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Security.Cryptography;
using System.Net;
using System.Net.Mail;
using System.Data;



namespace EmailNotificationService
{
    public partial class Service1 : ServiceBase
    {
        public Boolean done = false;
        public Service1()
        {
            InitializeComponent();
        }

        public void startthis()
        {
            this.OnStart(new string[0]);
        }

        protected override void OnStart(string[] args)
        {

            while (!done)
            {
                using (var context = new C335263_teacherharvestEntities())
                {
                    var user = from u in context.Students
                               where u.Completed == false
                               select u;

                    foreach (Students student in user)
                    {
                        string studentemail = "Devin.g.brown@gmail.com";
                
                        
                        string studentname = "Devin Brown";
                        string coursename = "CS3010 - Theory of Computation";
                        string uniquesalt = "s9npau5oog";

                        string emailmessage = "Dear Student, <br /><br /> You are currently enrolled in " + coursename + ". Please complete the following evaluation regarding your academic experience. <br /><br /> Navigate to www.fivescompliment.com/home/survey/" + uniquesalt + " to complete the evaluation.    <br /><br /> Your responses will be ANONYMOUS. We appreciate your participation! <br /><br /> - TeacherHarvest";

                        sendEmail(studentemail, "Please complete your Academic Survey(s) on TeacherHarvest", emailmessage); 
                    }

                }
                //go to sleep for a day
                done = true;
            }
        }

        protected override void OnStop()
        {
            done = true;
        }

        #region SendEmail

        public Boolean sendEmail(String Email, String Subject, String Message)
        {
            Boolean isSent = false;

            try
            {
                MailMessage mm = new MailMessage("webmaster@fivescompliment.com", Email);

                mm.Subject = Subject;
                mm.Body = Message;
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "mail.fivescompliment.com";
                //set EnambleSsl to True if using Gmail host
                smtp.EnableSsl = false;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "webmaster@fivescompliment.com";
                NetworkCred.Password = "Passw0rd12!";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);

                isSent = true;
            }
            catch (Exception ex)
            {
                string test = ex.ToString();
            }

            return isSent;
        }

        #endregion
    }
}
